//
//  GoodTeacherViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/12/6.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "GoodTeacherViewController.h"
#import "GoodTeacherTableViewCell.h"
#import "RatingView.h"
#import "RankModel.h"
#import "InfoViewController.h"

@interface GoodTeacherViewController ()
{
    NSMutableArray *_dataArray;
    RankModel *rankModel;
    NSInteger _index;

}
@end

@implementation GoodTeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    [_tableview.mj_header beginRefreshing];

}
- (void)headerRefreshing
{
    _index = 0;
    [self creatThirdHttpRequest];
}

- (void)footerRefreshing
{
    if (_dataArray.count%10 == 0) {
        _index ++;
        [self creatThirdHttpRequest];
    }else{
        [_tableview.mj_footer endRefreshing];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatThirdHttpRequest
{
    NSDictionary *param = @{
                            @"area ":@"",
                            @"pageIndex":[NSString stringWithFormat:@"%d",_index],
                            @"pageSize":@"20",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(QUERYTEACHERLEVEL) params:param complete:^(BOOL successed, NSDictionary *result) {
        NSLog(@"result===%@",result);
        if (successed) {
            [RankModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"RankData",
                         };
            }];
            rankModel = [[RankModel alloc] init];
            rankModel = [RankModel objectWithKeyValues:result];
            if (_dataArray.count==0) {
                _dataArray = [[NSMutableArray alloc] init];
                _dataArray=[NSMutableArray arrayWithArray:rankModel.data];
                
            }else{
                [_dataArray addObjectsFromArray:rankModel.data];
            }
            NSLog(@"%@",_dataArray);
            [_tableview reloadData];
            
            [_tableview.mj_header endRefreshing];
            [_tableview.mj_footer endRefreshing];
        }
        else{
            [_tableview.mj_header endRefreshing];
            [_tableview.mj_footer endRefreshing];
        }

    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RankData *data=_dataArray[indexPath.row];
    InfoViewController *vc=[[InfoViewController alloc]init];
    vc.memberId=data.member_Id;
    [self.navigationController pushViewController:vc animated:YES];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    GoodTeacherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"GoodTeacherTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
//
    RankData *data=_dataArray[indexPath.row];
    cell.lblTeaName.text=data.name;
    cell.lblZone.text=data.city;
    cell.lblTeachAge.text=data.seniority;
//    cell.lblPingFen.text=[NSString stringWithFormat:@"%@分",data.score];
    [cell.imgTeacherPhoto setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data.imgUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        cell.imgTeacherPhoto.image=image;
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    }];
    cell.lblPersonalSign.text=data.signature;
    cell.lblTeseClasss.text=data.special_programs;
    cell.lblRenzheng.text=[NSString stringWithFormat:@"%@项认证",data.certificationCount];
    cell.starRate.rating=data.score.intValue;
    return cell;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
